<!--培训计划启动-->
<template>
  <div class="page">
    <headerbar>
      培训计划启动
    </headerbar>
    <section class="bg-white pdh-2" v-if="form">

      <!--培训班信息-->
      <section class="block-head">培训班信息</section>
      <div class="pdh-2">
        <div class="input-column-wrap">
          <div class="name">培训计划名称：</div>
          <input class="noborder" readonly type="text" v-model="form.trainingclass">
        </div>

        <div class="input-column-wrap">
          <div class="name">班级名称：</div>
          <input type="text" v-model="form.className">
        </div>

        <div class="input-column-wrap">
          <div class="name">办班单位名：</div>
          <input readonly type="text" v-model="form.trainingclass">
        </div>

        <div class="input-column-wrap">
          <div class="name">培训机构名称：</div>
          <input readonly type="text" v-model="form.trainingclass">
        </div>

        <div class="input-column-wrap">
          <div class="name">期数：</div>
          <input class="noborder" readonly type="text" v-model="form.numberofperiods">
        </div>

        <div class="pdv-2"></div>
      </div>

      <!--课程信息-->
      <section class="block-head ">课程信息
        <div class="input-wrap">
          <div v-if="form.className" @click="addCourse" class="btn">+ 新增</div>
          <div v-if="!form.className" class="btn disable">+ 新增</div>
        </div>
      </section>
      <div class="pdh-2">
        <section class=" list-news-img list-wenda">
        <div class="item mgt-2 block" v-for="(item,index) in classList" :key="index">
          <div class="icon">
            <span class="icon-text">课</span>
          </div>
          <div class="cont">
            <h2 class="title">{{item.name}}</h2>
            <div class="tip">
              <span>课程时间： {{item.beginData}} - {{item.endData}}</span>
            </div>
            <div class="tip">
              <span>课程模块： {{item.modular}}</span>
              <span>课程名称： {{item.name}}</span>
              <span>学时： {{item.classHour}}</span>
            </div>
          </div>
        </div>
        <div class="pdv-2"></div>
        </section>
      </div>

      <!--人员信息-->
      <section class="block-head">人员信息
        <div class="input-wrap">
          <div @click="selectPerson" class="btn">+ 选择</div>
        </div>
      </section>
      <div class="pdh-2">

        <section class="people-list ">
          <div v-for="(item,index) in personList" :key="index" class="people-item block noborder">
            <section class="people-info">
              <div class="avatar-img">
                <img src="../../../../static/images/default.jpg" alt="">
              </div>
              <span class="name">{{item.userName}}</span>
            </section>
            <span @click="delPerson(index)" class="color-red">删除</span>
          </div>

        </section>
        <div class="pdv-3"></div>
        <div @click="save()" class="default-block-btn">完成</div>
        <div class="pdv-2"></div>
      </div>

    </section>
  </div>
</template>


<script>
  import _ from 'lodash'
  export default {
    name: 'trainingPlanStart',
    data () {
      return {
        form: {},
        classId: '',
        classList: [], // 课程列表
        personList: [], // 人员列表
        status: {
          0: '计划填报',
          1: '计划审核中',
          2: '计划审核通过',
          3: '计划审核不通过',
          4: '计划取消审核中',
          5: '计划取消审核通过',
          6: '计划取消审核不通过',
          7: '培训完成'
        },
        options: {
          pullUpLoad: true,
          scrollbar: false
        },
        pageIndex: 1,
        pageSize: 10,
        list: [],
        trainSchoolList: [],
        courseInstance: null
      }
    },
    created () {
      this.form = this.$store.state.trainingPlanDetail || {}
      this.loadData()
    },
    methods: {
      /*添加课程*/
      addCourse () {
        if (!this.courseInstance) {
          this.courseInstance = this.$createAddCourse({
            $events: {
              onSelect: data => {
                this.classList.push(data)
              }
            }
          })
        }
        this.courseInstance.show()
      },
      /*删除人员*/
      delPerson (index) {
        this.personList.splice(index, 1)
      },
      go (name) {
        this.$router.push({name: name})
      },
      /*选择对象*/
      selectPerson () {
        console.log(this)
        if (this.selectPersonInstance) {
          this.selectPersonInstance.show()
        } else {
          this.selectPersonInstance = this.$createPersonSelect({
            $events: {
              onSelect: list => {
                console.log('onSelect', list)
                this.personList = this.personList.concat(list)
              }
            }
          }).show()
        }
      },
      loadData () {
      },
      save () {
        this.trainingplanClassSave().then(res => {
          let classId = res.data.id
          this.classId = classId
          this.TrainingplanUserContactSave(res.data.id).then(res => {
              this.trainingplanCurriculumSave(classId).then(res => {
                this.trainingplanClassUpdateById()
              })
          })
        })
      },
      // 培训班新增或更新
      trainingplanClassSave () {
        let params = {
          className: this.form.className,
          deptId: this.form.deptId,
          deptid: this.form.deptid,
          deptname: this.form.deptname,
//          id: this.form.id,
          institutionsId: this.form.institutionsId,
          institutionsName: this.form.institutionsName,
          numberofperiods: this.form.numberofperiods,
          pxfaFileId: this.form.pxfaFileId,
          pxfaFileName: this.form.pxfaFileName,
          pxzjCzwt: this.form.pxzjCzwt,
          pxzjFileId: this.form.pxzjFileId,
          pxzjFileName: this.form.pxzjFileName,
          pxzjGzjy: this.form.pxzjGzjy,
          pxzjJbqk: this.form.pxzjJbqk,
          pxzjJycx: this.form.pxzjJycx,
          pxzjZyzf: this.form.pxzjZyzf,
          trainingclass: this.form.trainingclass,
          zjspr: this.form.zjspr,
          zjspsj: this.form.zjspsj,
          zjspzt: this.form.zjspzt,
          pxjhId: this.form.id,
          sfzc: '1',
          type: '1'
        }
//        params = _.extend(params, this.form)
        this.tools.showLoading('数据提交中...')
        return this.fetchService.trainingplanClassSave(params).then(res => {
          if (res.code !== 0) {
            return this.tools.tip(res.message || '接口服务出错，请联系管理员')
          }
          return res
        })
      },
      // 培训班参与人员批量保存
      TrainingplanUserContactSave (classId) {
        let params = []
        let personDemo = {
          classId: classId,
          groupId: "999010019061002",
          groupName: "政府办二支部",
          id: "16cd16a3-79d3-4479-a5cd-df234da78ab6",
          leaveList: null,
          pjList: null,
          pxzjFileid: null,
          pxzjFilename: null,
          sfdy: null,
          state: "0",
          userId: "259325",
          userName: "钟燕"
        }
        this.personList.forEach(v => {
          params.push(_.extend(personDemo, v))
        })
        params = _.uniq(params)
        return this.fetchService.TrainingplanUserContactSave(params).then(res => {
          if (res.code !== 0) {
            return this.tools.tip(res.message || '接口服务出错，请联系管理员')
          }
          return res
        })
      },
      // 培训班关联的课程表批量保存
      trainingplanCurriculumSave (classId) {
        let params = []
        let personDemo = {
          beginData: "2020-07-08 06:30:00",
          classHour: 3.5,
          classId: classId,
          className: "",
          endData: "2020-07-08 10:00:00",
          institutionsId: "4",
          lecturerId: "e37129cc3ad447c29af119665e6003a0",
          lecturerName: "对的",
          modular: "33",
          name: "33",
          state: 0
        }
        this.classList.forEach(v => {
          params.push(_.extend(personDemo, v))
        })
        return this.fetchService.trainingplanCurriculumSave(params).then(res => {
          if (res.code !== 0) {
            return this.tools.tip(res.message || '接口服务出错，请联系管理员')
          }
          return res
        })
      },
      // 培训班根据主键id修改不为空的字段
      trainingplanClassUpdateById () {
        let params = {"id": this.classId, "type": 1, "sfzc": 1}
        this.fetchService.trainingplanClassUpdateById(params).then(res => {
          this.tools.hideLoading()
          if (res.code !== 0) {
            return this.tools.tip(res.message || '接口服务出错，请联系管理员')
          }
          this.tools.alert('提交成功')
        })
      },
      // 获取培训机构
      groupQueryList () {
        let params = {
          dimId: 4,
          depth: 1
        }
        this.fetchService.groupQueryList(params).then(res => {
          if (res.code !== 0) {
            return this.tools.tip(res.message || '接口服务出错，请联系管理员')
          }
          this.trainSchoolList = res.data
        })
      }
    }
  }
</script>

<style scoped>

</style>
